<!--
 * @Author: Bennty
 * @Date: 2024-12-18 10:41:43
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-12-18 15:30:43
 * @Description: 请填写简介
-->
<template>
    <el-dialog :model-value="props.visible" title="地址信息" width="35%" @close="handleClose">
        <el-descriptions :column="2">
            <el-descriptions-item label="产品类型：">{{ customized_order_type.filter((item:any)=>props.orderDetails.productType == item.value)[0]?.label }}</el-descriptions-item>
            <el-descriptions-item label="制单日期：">{{ props.orderDetails.createTime }}</el-descriptions-item>
            <el-descriptions-item label="制单员：">{{ props.orderDetails.createByName }}</el-descriptions-item>
            <el-descriptions-item label="logo名称：">{{  }}</el-descriptions-item>
            <el-descriptions-item label="产品名称：">{{  }}</el-descriptions-item>
            <el-descriptions-item label="联系电话：">{{  }}</el-descriptions-item>
        </el-descriptions>
        <el-form ref="formRef" :model="formData">
            <el-form-item label="地址识别">
                <el-input placeholder="请输入" v-model="addressIdentify" style="width: 300px">
                    <template #append>
                        <el-button :icon="Search" @click="handleClickAnalysis" />
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item label="客户地址">
                <el-cascader filterable clearable :options="pcaTextArr" v-model="selectedOptions"></el-cascader>
            </el-form-item>
            <el-form-item label="地址详情" prop="detailAddress" >
                <el-input placeholder="请输入" v-model="formData.detailAddress" style="width: 300px"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" @click="handleSubmit" >确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { analysisAddressApi } from '@/api/sj/customer-crm/index';
import { addAddressApi,getAddressByIdApi, editAddressApi } from '@/api/sj/productManagement/customizedOrder';
import { pcaTextArr} from 'element-china-area-data';
import type { FormInstance } from 'element-plus';
import { Search } from '@element-plus/icons-vue';

const props = defineProps<{
    visible: boolean,
    orderDetails: any,
    customized_order_type: any[]
}>();
const emits = defineEmits(['close', 'submit']);
const formRef = ref<FormInstance>();
const addressIdentify = ref<string | undefined>(undefined);
const formData = ref({
    detailAddress: undefined
});
const selectedOptions = ref<any[]>([]);
const isEdit = ref(false);
const editId = ref<string | number | undefined>();

watch(()=>[props.visible, props.orderDetails],async ([newVisible, newData])=>{
    if(newVisible){
        const {data} = await getAddressByIdApi(newData.id);
        if(!data){
            isEdit.value = false;
        }else{
            isEdit.value = true;
            editId.value = data.id;
            selectedOptions.value[0] = data.province;
            selectedOptions.value[1] = data.city;
            selectedOptions.value[2] = data.county;
            formData.value.detailAddress = data.detailAddress;
        }
    }
});

const analysisAddress = async ()=>{
    const { data } = await analysisAddressApi({address:addressIdentify.value});
    selectedOptions.value[0] = data.province;
    selectedOptions.value[1] = data.city;
    selectedOptions.value[2] = data.county; 
    formData.value.detailAddress = data.detail
};

const handleClickAnalysis = () => {
    analysisAddress();
};

const handleSubmit = async () => {
    
    const [province, city, county] = selectedOptions.value;
    
    if(!isEdit.value){
        const submitData = {
            province,
            city,
            county,
            ...formData.value,
            businessId: props.orderDetails.id
        };
        const{ code } = await addAddressApi(submitData);
        if(code == 200){
            handleClose();
            emits('submit', true);
        }
    }else{
        const submitData = {
            province,
            city,
            county,
            ...formData.value,
            businessId: props.orderDetails.id,
            id: editId.value
        };
        const { code } = await editAddressApi(submitData); 
        if(code == 200){
            handleClose();
            emits('submit', true);
        };
    }
};

const handleClose = () => {
    selectedOptions.value = [];
    addressIdentify.value = undefined;
    emits('close')
};
</script>

<style scoped>

</style>